<?php
defined('ACCESS_ZYG') or exit('access invalid');
class loginAction extends Action{
    public function indexOp(){
        if(isset($_POST['submit_admin'])){
            $admin_name = trim($_POST['user_name']);
            $admin_password = trim($_POST['pwd']);
            $captcha = trim($_POST['captcha']);
            $login_escape = isset($_POST['login_escape'])?$_POST['login_escape']:0;
            /*此处省略合法性验证*/

            //有效性验证
            $admin_obj = new adminModel();
            $admin_row = $admin_obj->checkAdmin($admin_name,$admin_password);
            if(!$admin_row){
                $this->showMessage('用户名或密码不正确','index.php?act=login');
            }
            if(!Captcha::checkCaptcha($captcha)){
                $this->showMessage('验证码不正确','index.php?act=login');
            }
            /*验证通过，将用户信息记录session*/
            $_SESSION['is_login'] = 1;
            $_SESSION['admin_info'] = $admin_row;
            $admin_obj->updateInfo($admin_row['admin_id']);
            if($login_escape){
                //用户7天都处于登陆状态
                setcookie('login_escape',$admin_row['admin_id'],time()+3600*24*7,'/');
            }
            $this->redirect('index.php?act=index&op=index');

        }else{
            include_once TEMPLATES_DIR.DS."login.html";
        }
    }
    public function captchaOp(){
        $captcha_obj = new Captcha();
        $captcha_obj->createCaptcha();
    }
    public function quitOp(){
        setcookie('login_escape',0,time()-1,'/');
        session_destroy();

        $this->redirect('index.php');
    }
}